package com.mycom.controller;

import com.mycom.controller.constant.Code;
import com.mycom.controller.entity.Result;
import com.mycom.pojo.Department;
import com.mycom.pojo.DepartmentEmployee;
import com.mycom.pojo.Employee;
import com.mycom.service.DepartmentService;
import com.mycom.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/departmentEmployees")
public class DepartmentEmployeeController {
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private EmployeeService employeeService;

    @GetMapping
    public Result findAllDepartmentEmployee(){
        List<DepartmentEmployee> departmentEmployees = departmentService.findAllDepartmentEmployees();
        for (DepartmentEmployee departmentEmployee : departmentEmployees) {
            List<Employee> employees = employeeService.findAllByDepartmentId(departmentEmployee.getId());
            departmentEmployee.setChildren(employees);
        }
        Integer code = departmentEmployees!=null ? Code.QUERY_ALL_SUCCESS : Code.QUERY_ALL_ERR;
        String msg = departmentEmployees!=null ? "查询所有科室成员成功！" : "查询所有科室成员失败！";
        return new Result(code,departmentEmployees,msg);
    }

//    @GetMapping
//    public Result findAll(){
//        List<Department> departments = departmentService.findAll();
//        for (Department department : departments) {
//            List<Employee> employees = employeeService.findAllByDepartmentId(department.getId());
//            department.setEmployees(employees);
//        }
//        Integer code = departments!=null ? Code.QUERY_ALL_SUCCESS : Code.QUERY_ALL_ERR;
//        String msg = departments!=null ? "查询所有科室成员成功！" : "查询所有科室成员失败！";
//        return new Result(code,departments,msg);
//    }
}
